﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using Iris.Engine;
using System.Threading;
using System.Windows.Threading;
using System.Windows.Media.Animation;

namespace Iris.UI
{
    /// <summary>
    /// Logique d'interaction pour Duel2Window.xaml
    /// </summary>
    public partial class Duel2Window : Window
    {
        #region statiques

        public void SetImage(Image image, ImageSort imageSort)
        {
            image.Source = imageSort.presentation;
            FadeIn(image);
        }

        #endregion

        private ImageSort imageMetaA;
        private ImageSort imageMetaB;

        public Duel2Window()
        {
            InitializeComponent();
        }

        private void on_LoadDuelPlusDeDuelEvent()
        {
            new NotificationWindow("Tri terminé : Sauvez les notes.").Show();
            Thread.Sleep(1000);
            this.Close();
        }

        private void on_LoadDuelAucuneImageEvent()
        {
            new NotificationWindow("Selectionner un dossier").Show();
            Thread.Sleep(1000);
            this.Close();
            
        }

        private void on_LoadDuelEvent(ImageSort a, ImageSort b, int progression)
        {
            progressBarDuel.Value = progression;
            imageMetaA = a;
            imageMetaB = b;
            SetImage(this.image1, imageMetaA);
            SetImage(this.image2, imageMetaB);
            this.Cursor = Cursors.Arrow;
        }

        void ImageFadeOut_Completed(object sender, EventArgs e)
        {
            SortCore.singleton.BeginLoadDuel();
        }

        private void FadeOut(Image image) {
            Storyboard AnimFadeOut = new Storyboard();
            AnimFadeOut.Completed += new EventHandler(ImageFadeOut_Completed);

            DoubleAnimation FadeOut = new DoubleAnimation();
            FadeOut.From = 1.0;
            FadeOut.To = 0.0;
            FadeOut.Duration = new Duration(TimeSpan.FromSeconds(.2));

            AnimFadeOut.Children.Add(FadeOut);
            Storyboard.SetTargetName(FadeOut, image.Name);
            Storyboard.SetTargetProperty(FadeOut, new PropertyPath(Image.OpacityProperty));
            AnimFadeOut.Begin(this);
        }

        private void FadeIn(Image image) {
            Storyboard Anin = new Storyboard();
            //Anin.Completed += new EventHandler(ImageFadeOut_Completed);

            DoubleAnimation FadeOut = new DoubleAnimation();
            FadeOut.From = 0.0;
            FadeOut.To = 1.0;
            FadeOut.Duration = new Duration(TimeSpan.FromSeconds(.2));

            Anin.Children.Add(FadeOut);
            Storyboard.SetTargetName(FadeOut, image.Name);
            Storyboard.SetTargetProperty(FadeOut, new PropertyPath(Image.OpacityProperty));
            Anin.Begin(this);
        }

        private void OnKeyDownHandler(object sender, KeyEventArgs e)
        {
            if (Keyboard.IsKeyDown(Key.LeftCtrl) || Keyboard.IsKeyDown(Key.RightCtrl))
            {
                if (e.Key == Key.Left)
                {
                    BlamerA();
                }
                else if (e.Key == Key.Right)
                {
                    BlamerB();
                }
                else if (e.Key == Key.Up)
                {
                    BlamerAB();
                }
            }
            else
            {
                if (e.Key == Key.Left)
                {
                    VoterA();
                }
                else if (e.Key == Key.Right)
                {
                    VoterB();
                }
                else if (e.Key == Key.Up)
                {
                    VoterAB();
                }
                else if (e.Key == Key.Escape)
                {
                    this.Close();
                }
            }
        }

        private void VoterAB()
        {
            this.Cursor = Cursors.Wait;
            SortCore.singleton.EndLoadDuelNull(imageMetaB, imageMetaA);
            SortCore.singleton.BeginLoadDuel();
        }

        private void VoterB()
        {
            this.Cursor = Cursors.Wait;
            FadeOut(image1);
            SortCore.singleton.EndLoadDuel(imageMetaB, imageMetaA);
        }

        private void VoterA()
        {
            this.Cursor = Cursors.Wait;
            FadeOut(image2);
            SortCore.singleton.EndLoadDuel(imageMetaA, imageMetaB);
        }

        
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            SortCore.singleton.LoadDuelEvent += on_LoadDuelEvent;
            SortCore.singleton.LoadDuelAucuneImageEvent += on_LoadDuelAucuneImageEvent;
            SortCore.singleton.LoadDuelPlusDeDuelEvent += on_LoadDuelPlusDeDuelEvent;
            SortCore.singleton.BeginLoadDuel();
        }

        private void Window_Closed(object sender, EventArgs e)
        {
            SortCore.singleton.LoadDuelEvent -= on_LoadDuelEvent;
            SortCore.singleton.LoadDuelAucuneImageEvent -= on_LoadDuelAucuneImageEvent;
            SortCore.singleton.LoadDuelPlusDeDuelEvent -= on_LoadDuelPlusDeDuelEvent;
        }

        private void BlamerA()
        {
            FadeOut(image2);
            SortCore.singleton.BlameImage(imageMetaA);
            SortCore.singleton.EndLoadDuel(imageMetaB, imageMetaA);
            
        }

        private void BlamerB()
        {
            FadeOut(image1);
            SortCore.singleton.BlameImage(imageMetaB);
            SortCore.singleton.EndLoadDuel(imageMetaA, imageMetaB);
        }

        private void BlamerAB()
        {
            SortCore.singleton.BlameImage(imageMetaB);
            SortCore.singleton.BlameImage(imageMetaA);
            SortCore.singleton.EndLoadDuelNull(imageMetaA, imageMetaB);
            SortCore.singleton.BeginLoadDuel();
        }

        private void buttonAide_Click(object sender, RoutedEventArgs e)
        {
            if (panelAide.Visibility != System.Windows.Visibility.Visible)
            {
                panelAide.Visibility = System.Windows.Visibility.Visible;
            }
            else {
                panelAide.Visibility = System.Windows.Visibility.Collapsed;
            }
        }

        private void helpDuel1_OnClose(object sender, MouseButtonEventArgs e)
        {
            panelAide.Visibility = System.Windows.Visibility.Collapsed;
        }
    }
}
